<template>
	<div>
		<el-col :span="1.5"><div style="line-height: 30px; font-size: 15px; color: #666">生成表单</div></el-col>
		<el-col :span="1.5">
			<el-select size="small" v-model="selval" @change="downloadFile()">
				<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
			</el-select>
		</el-col>
	</div>
</template>
<script>
import formdata from "@/data/createFormData"
import { downloadWithName, downloadFile } from "@/utils/requestBlob"
export default {
	name: "CreateForm",
	props: {
		special: {
			type: Boolean,
			required: false,
			default: false
		},
		repairProductId: {
			type: Array,
			required: false,
			default: () => {
				return []
			}
		},
		formtype: {
			type: String,
			required: true,
			default: ""
		},
		ids: {
			type: Array,
			default: () => {
				return []
			}
		},
		// type=8, 跨父级选择的时候需要判断的字段
		parentId: {
			type: String,
			required: false,
			default: ""
		},
		rows: {
			type: Array,
			default: () => {
				return []
			}
		}
	},
	data() {
		return {
			selval: undefined,
			options: [],
			idsreplace: undefined // 替换ids
		}
	},
	created() {
		this.options = formdata[this.formtype]
	},
	methods: {
		// 生成文件下载
		async downloadFile() {
			this.idsreplace = undefined
			let params = {}
			let cur = this.options.find(item => {
				return item.id == this.selval
			})
			let url = cur.url
			// 有传参
			if (cur.type != 0 && cur.type != 5) {
				let msg = this.checkSelect(cur)
				if (msg) {
					this.$modal.msgError(msg)
					this.clearval()
					return false
				}
				let ids = this.idsreplace ?? this.ids
				url += ids
			}
			if (cur.type != 5) {
				downloadWithName(url, params).finally(() => {
					this.clearval()
				})
			} else {
				downloadFile("/bhgsldDc/exportgzhsWord4", { ids: this.ids }).finally(() => {
					this.clearval()
				})
			}
		},
		// 检查勾选项
		checkSelect(cur) {
			//是否有选中
			if (this.ids.length == 0) {
				return "请选择记录"
			}
			// 单选1，父级单选3，子级单选4
			if (cur.type == 1 || cur.type == 3 || cur.type == 4) {
				if (this.ids.length > 1) {
					return "只能选择一条记录"
				}
			}
			// 不同父级下子级，这里保留选择父级自动导出子级全部数据
			if (cur.type == 2) {
				// 获取子级数据
				let curs = this.rows.filter(item => {
					return String(item.id).indexOf("c") == 0
				})
				let ids = []
				curs.forEach(item => {
					ids.push(item.id.replace("c", ""))
				})
				if (ids.length == 0) {
					return "请选择子级记录"
				}
				this.idsreplace = ids
			}
			// 单选第一个id
			let cid = String(this.ids[0])
			// 父级单选
			if (cur.type == 3) {
				if (cid.indexOf("c") == 0) {
					return "请选择父级记录"
				}
			}
			// 子级单选
			if (cur.type == 4) {
				if (cid.indexOf("c") != 0) {
					return "请选择子级记录"
				}
				if (this.special) {
					this.idsreplace = String(this.repairProductId[0])
				} else {
					this.idsreplace = cid.replace("c", "")
				}
			}
			// 父级多选
			if (cur.type == 7) {
				// 获取父级数据
				let curs = this.rows.filter(item => {
					return String(item.id).indexOf("c") != 0
				})
				let ids = []
				curs.forEach(item => {
					ids.push(item.id)
				})
				if (ids.length == 0) {
					return "请选择父级记录"
				}
				this.idsreplace = ids
			}
			// 相同父级下子级，这里保留选择父级自动导出子级全部数据（不能跨父级）
			if (cur.type == 8) {
				// 获取子级数据
				let curs = this.rows.filter(item => {
					return String(item.id).indexOf("c") == 0
				})
				let ids = []
				let parentSet = new Set()
				curs.forEach(item => {
					ids.push(item.id.replace("c", ""))
					parentSet.add(item[this.parentId])
				})
				if (ids.length == 0) {
					return "请选择子级记录"
				}
				if (parentSet.size > 1) {
					return "不能跨父级选择"
				}
				this.idsreplace = ids
			}
			return false
		},
		// 重置
		clearval() {
			this.selval = undefined
		}
	}
}
</script>

<style lang="scss" scoped></style>
